

/**
* 床位配置
*/

Choose = {
	buildId:""
	,buildName:""
	,floorId:""
	,floorName:""
	,roomId:""
	,roomNumber:""
	,roomType:""
	,bedId:""
	,bedNumber:""
	,showDomId:"#chooseInfo"
	,isShowInfo:false
	,bedStatus:null  //床位状态  1-已入住  0-未入住  null 所有
	,showChoose:function(){
		$(this.showDomId).empty();
		$(this.showDomId).append(this.buildName+" 栋 " + this.roomNumber +" 室 "+this.bedNumber+" 床 "+"（"+this.getRoomType()+"）");
	}
	,getRoomType:function(){
		if(this.roomType == 1){
			return "单人房";
		}
		if(this.roomType == 2){
			return "双人房";
		}
		return "多人房";
	}
}

Build = {
	buildId:""
	,optType:1  // 1 列表 2 编辑
	,edit:function(){
		parent.Tab.openTab('build_edit', '楼栋编辑', contentPath+"/build/toEdit?buildId="+this.buildId);
	}
	,del:function(){
		var isCanDel = false;
		$.ajax({
            url: contentPath+"/build/isCanDel/info",
            data: {
            	buildId: this.buildId
            },
            type: "post",
            dataType: "json",
            async:false,
            success: function (data) {
                ae.close_all();
                if (data.ecode == 1000) {
                	isCanDel = data.data;
                } else {
                	ae.warn(data.massage);
                }
            }
        });
		if(!isCanDel){
			ae.warn("该楼栋存在有效数据，不允许删除");
		}else{
			ae.confirm('您是否需删除该楼栋?', function() {
				$.ajax({
	                url: contentPath+"/build/del/info",
	                data: {
	                	buildId: Build.buildId
	                },
	                type: "post",
	                dataType: "json",
	                success: function (data) {
	                    ae.close_all();
	                    if (data.ecode == 1000) {
	                    	ae.success(data.massage);
	                    } else {
	                    	ae.warn(data.massage);
	                    }
	                    window.setTimeout(function() {
		                    window.location.reload(true);
						}, 2000);
	                }
	            });
			}, function() {
				return false;
			});
		}
	}
	,buildInfo:function(){
		$.ajax({
            url: contentPath+"/build/list/floor",
            data: {
            	buildId: this.buildId
            },
            type: "post",
            dataType: "json",
            success: function (data) {
            	$("#floor").empty();
            	$("#roomUl").empty();
            	$("#bedUl").empty();
                ae.close_all();
                if (data.ecode == 1000) {
                	var floors = data.data;
                	if(floors==null){
                		return ;
                	}
                	for (var i = 0; i < floors.length; i++) {
            			$("#floor").append("<button class='layui-btn layui-btn-xs layui-btn-primary' style='width:100%;margin: 1px' onclick=Floor.room("+floors[i].id+",this) floorName="+floors[i].floorName+">"+floors[i].floorName+"</button>");
					}
                	$("#floor button:eq(0)").click();
                } else {
                	ae.warn(data.massage);
                }
            }
        });
	}
}


Floor={
	add:function(){
		if(Build.buildId == null){
			ae.warn("请选择需要添加的楼栋");
			return false;
		}
		layer.open({
			type: 2,
			content: contentPath+"/build/toEdit/floor?buildId="+Build.buildId
			,end:function(){
                window.location.reload(true);
			}
			,area: ['300px', '220px']
		});
	}
	,edit:function(floorId){
		layer.open({
			type: 2, 
			content:  contentPath+"/build/toEdit/floor?floorId="+floorId+"&buildId="+Build.buildId
			,end:function(){
                window.location.reload(true);
			}
			,area: ['300px', '220px']
		});
	}
	,del:function(floorId){
		var isCanDel = false;
		$.ajax({
            url: contentPath+"/build/isCanDel/floor",
            data: {
            	floorId: floorId
            },
            type: "post",
            dataType: "json",
            async:false,
            success: function (data) {
                ae.close_all();
                if (data.ecode == 1000) {
                	isCanDel = data.data;
                } else {
                	ae.warn(data.massage);
                }
                return false;
            }
        });
		if(!isCanDel){
			ae.warn("该楼层存在有效数据，不允许删除");
			return;
		}
		ae.confirm('您是否需删除该楼层?', function() {
			$.ajax({
                url: contentPath+"/build/del/floor",
                data: {
                	floorId: floorId
                },
                type: "post",
                dataType: "json",
                success: function (data) {
                    ae.close_all();
                    if (data.ecode == 1000) {
                    	ae.success(data.massage);
                    } else {
                    	ae.warn(data.massage);
                    }
                    window.setTimeout(function() {
	                    window.location.reload(true);
					}, 2000);
                }
            });
		}, function() {
			return false;
		});
	}
	,room:function(floorId,obj){
		if(Build.optType == 1){
			$(obj).siblings().removeClass("active");
			$(obj).addClass("active");
			Choose.floorName = $(obj).attr("floorName");
			Choose.roomId = "";
			Choose.roomNumber = "";
			Choose.roomType = "";
			Choose.bedId = "";
			Choose.bedNumber = "";
			$(Choose.showDomId).empty();
		}else{
			$(obj).closest("li").siblings().removeClass("active");
			$(obj).closest("li").addClass("active");
		}
		Room.floorId = floorId;
		Choose.floorId = floorId;
		$.ajax({
            url: contentPath+"/build/list/room",
            data: {
            	floorId: floorId
            },
            type: "post",
            dataType: "json",
            success: function (data) {
                ae.close_all();
                $("#roomUl").empty();
                $("#bedUl").empty();
                if (data.ecode == 1000) {
                	var rooms = data.data;
                	if(rooms==null){
                		return ;
                	}
                	for (var i = 0; i < rooms.length; i++) {
                		if(Build.optType ==1 ){
                			$("#roomUl").append('<button class="layui-btn layui-btn-xs layui-btn-primary" style="width:100%;margin: 1px"  onclick="Room.beds('+rooms[i].id+',this)" roomNumber = '+rooms[i].roomNumber+' roomType = '+rooms[i].roomType+'>'+rooms[i].roomName+'</button>');
                			$("#roomUl button:eq(0)").click();
                		}else{
                			$("#roomUl").append('<li><a class="layui-btn layui-btn-xs layui-btn-primary"  style="display: inline-block; padding: 0 100px;" onclick="Room.beds('+rooms[i].id+',this)" title="查看床位">'+rooms[i].roomName+'</a><span><i class="layui-icon layui-btn layui-btn-xs layui-btn-primary " onclick="Room.edit('+rooms[i].id+')" title="编辑">&#xe642;</i><i class="layui-icon layui-btn layui-btn-xs layui-btn-primary" onclick="Room.del('+rooms[i].id+');" title="删除">&#x1006;</i></span></li>');
                			$("#roomUl li:eq(0)").click();
                		}
					}
                } else {
                	ae.warn(data.massage);
                }
            }
        });
	}
}
	
	
Room={
	floorId:null
	,add:function(){
		if(this.floorId == null){
			ae.warn("请选择需要添加的楼层");
			return false;
		}
		layer.open({
			type: 2,
			content: contentPath+"/build/toEdit/room?floorId="+this.floorId
			,end:function(){
                
			}
			,area: ['600px', '300px']
		});
	}
	,edit:function(roomId){
		layer.open({
			type: 2, 
			content:  contentPath+"/build/toEdit/room?floorId="+this.floorId+"&roomId="+roomId
			,end:function(){
				window.location.reload(true);
			}
			,area: ['600px', '300px']
		});
	}
	,del:function(roomId){
		var isCanDel = false;
		$.ajax({
            url: contentPath+"/build/isCanDel/room",
            data: {
            	roomId: roomId
            },
            type: "post",
            dataType: "json",
            async:false,
            success: function (data) {
                ae.close_all();
                if (data.ecode == 1000) {
                	isCanDel = data.data;
                } else {
                	ae.warn(data.massage);
                }
                return false;
            }
        });
		if(!isCanDel){
			ae.warn("该房间存在有已入住床位，不允许删除");
			return;
		}
		ae.confirm('您是否需删除该房间?', function() {
			$.ajax({
                url: contentPath+"/build/del/room",
                data: {
                	roomId: roomId
                },
                type: "post",
                dataType: "json",
                success: function (data) {
                    ae.close_all();
                    if (data.ecode == 1000) {
                    	ae.success(data.massage);
                    } else {
                    	ae.warn(data.massage);
                    }
                    window.setTimeout(function() {
	                    window.location.reload(true);
					}, 2000);
                }
            });
		}, function() {
			return false;
		});
	}
	,beds:function(roomId, obj){
		if(Build.optType == 1){
			$(obj).siblings().removeClass("active");
			$(obj).addClass("active");
			Choose.roomId = roomId;
			Choose.roomNumber = $(obj).attr("roomNumber");
			Choose.roomType = $(obj).attr("roomType");
			Choose.bedId = "";
			Choose.bedNumber = "";
			$(Choose.showDomId).empty();
		}else{
			$(obj).closest("li").siblings().removeClass("active");
			$(obj).closest("li").addClass("active");
		}
		$.ajax({
            url: contentPath+"/build/list/bed",
            data: {
            	roomId: roomId
            },
            type: "post",
            dataType: "json",
            success: function (data) {
                ae.close_all();
                $("#bedUl").empty();
                if (data.ecode == 1000) {
                	var beds = data.data;
                	if(beds==null){
                		return ;
                	}
                	for (var i = 0; i < beds.length; i++) {
                		var status = beds[i].status==1?"已入住":"未入住";
                		if(Choose.bedStatus == null || Choose.bedStatus == undefined){
                			if(Build.optType ==1 ){
                				$("#bedUl").append('<button class="layui-btn layui-btn-xs layui-btn-primary" style="width:100%;margin: 1px" onclick="Bed.chooseBed('+beds[i].id+',this)" bedNumber='+beds[i].bedNumber+'>'+beds[i].bedNumber+' - （'+status+'）</button>');
                			}else{
                				$("#bedUl").append('<li class="layui-btn layui-btn-xs layui-btn-primary" style="width:100%;margin-left: 0px;margin-top:1px" >'+beds[i].bedNumber+' - （'+status+'）</li>');
                			}
                		}else{
                			if(Choose.bedStatus == beds[i].status){
                				if(Build.optType ==1 ){
                    				$("#bedUl").append('<button class="layui-btn layui-btn-xs layui-btn-primary" style="width:100%;margin: 1px" onclick="Bed.chooseBed('+beds[i].id+',this)" bedNumber='+beds[i].bedNumber+'>'+beds[i].bedNumber+' - （'+status+'）</button>');
                    			}else{
                    				$("#bedUl").append('<li class="layui-btn layui-btn-xs layui-btn-primary" style="width:100%;margin-left: 0px;margin-top:1px" >'+beds[i].bedNumber+' - （'+status+'）</li>');
                    			}
                			}
                		}
					}
                } else {
                	ae.warn(data.massage);
                }
            }
        });
	}
}

Bed={
	chooseBed:function(bedId, obj){
		if(Build.optType == 1){
			$(obj).siblings().removeClass("active");
			$(obj).addClass("active");
		}else{
			$(obj).closest("li").siblings().removeClass("active");
			$(obj).closest("li").addClass("active");
		}
		Choose.bedId = bedId;
		Choose.bedNumber = $(obj).attr("bedNumber");
		if(Choose.isShowInfo){
			Choose.showChoose();
		}
	}
}

